import javax.swing.*;
import java.awt.*;

public class gui extends JFrame {
    public gui() {
        setTitle("复杂布局示例");
        setSize(600, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // 主面板使用 BorderLayout
        JPanel mainPanel = new JPanel(new BorderLayout());
        add(mainPanel);

        // 1. 顶部工具栏（FlowLayout）
        JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        topPanel.add(new JButton("新建"));
        topPanel.add(new JButton("打开"));
        mainPanel.add(topPanel, BorderLayout.NORTH);

        // 2. 左侧导航栏（GridLayout）
        JPanel leftPanel = new JPanel(new GridLayout(4, 1, 5, 5));
        leftPanel.add(new JButton("主页"));
        leftPanel.add(new JButton("设置"));
        leftPanel.add(new JButton("帮助"));
        leftPanel.add(new JButton("退出"));
        mainPanel.add(leftPanel, BorderLayout.WEST);

        // 3. 中心区域（GridBagLayout）
        JPanel centerPanel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5); // 外边距

        // 第一行
        gbc.gridx = 0; gbc.gridy = 0;
        centerPanel.add(new JLabel("用户名:"), gbc);
        gbc.gridx = 1;
        centerPanel.add(new JTextField(15), gbc);

        // 第二行
        gbc.gridx = 0; gbc.gridy = 1;
        centerPanel.add(new JLabel("密码:"), gbc);
        gbc.gridx = 1;
        centerPanel.add(new JPasswordField(15), gbc);

        // 第三行（跨两列）
        gbc.gridx = 0; gbc.gridy = 2;
        gbc.gridwidth = 2;
        gbc.anchor = GridBagConstraints.CENTER;
        centerPanel.add(new JButton("登录"), gbc);

        mainPanel.add(centerPanel, BorderLayout.CENTER);

        // 4. 底部状态栏（BoxLayout）
        JPanel bottomPanel = new JPanel();
        bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.X_AXIS));
        bottomPanel.add(new JLabel("状态: "));
        bottomPanel.add(Box.createHorizontalGlue()); // 弹性空间
        bottomPanel.add(new JLabel("就绪"));

        mainPanel.add(bottomPanel, BorderLayout.SOUTH);

        setVisible(true);
    }

    public static void main(String[] args) {
        new gui();
    }
}

